const crypto = require('crypto');
var request = require('request')
const com = require('./index.js');
const RApiurl = 'http://api-cn.ronghub.com/'
const RAppkey = 'tdrvipkstxtp5'
const RAppSecret = 'o057FyMRaa'

module.exports = class rongyun {
    constructor() {}
    /*融云封装请求接口*/
    RyunAjax(url, data) {
        return new Promise((resolve, reject) => {
            const cm = new com()
            const appSecret = RAppSecret
            const nonce = cm.randomString(18)
            const timestamp = new Date().getTime()
            const signature = crypto.createHash('sha1').update(appSecret + nonce + timestamp).digest('hex')
            let zh = cm.jsonToString(data)
            console.log(zh);
            request({
                url: `${RApiurl}${url}?${zh}`,
                method: 'POST',
                headers: {
                    'App-Key': RAppkey,
                    'Nonce': nonce,
                    'Timestamp': timestamp,
                    'Signature': signature
                },
            }, function (err, response, body) {
                console.log(err);
                resolve(JSON.parse(body))
            });
        })
    };
}
